package com.sl.mes.db.service.mm;

import com.sl.mes.db.dao.MmReqpickmBMapper;
import com.sl.mes.db.dao.MmReqpickmMapper;
import com.sl.mes.db.itf.mm.IMmReqpickm;
import com.sl.mes.db.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MmReqpickmDaoService implements IMmReqpickm {

    private final MmReqpickmB.Column[] result = new MmReqpickmB.Column[]
            { MmReqpickmB.Column.creqpickmBid,MmReqpickmB.Column.cmoid,MmReqpickmB.Column.vbdef1,
                    MmReqpickmB.Column.vbdef10,MmReqpickmB.Column.vbdef11};

    @Resource
    private MmReqpickmMapper  mmReqpickmMapper;

    @Resource
    private MmReqpickmBMapper mmReqpickmBMapper;

    /**
     * 根据id查询配料单
     * @param creqpickmid
     * @return
     */
    @Override
    public MmReqpickm findParentVoById(String creqpickmid) {
        return mmReqpickmMapper.selectByPrimaryKey(creqpickmid);
    }

    @Override
    public List<MmReqpickmB> findChildrenVoById(String creqpickmid) {
        MmReqpickmBExample example = new MmReqpickmBExample();
        MmReqpickmBExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(creqpickmid)) {
            criteria.andCreqpickmidEqualTo(creqpickmid);
        }
        return mmReqpickmBMapper.selectByExample(example);

    }

    /**
     * 配料单的删除
     * @param creqpickmid
     * @return
     */
    @Override
    public int deleteParentVoById(String creqpickmid) {
       return mmReqpickmMapper.deleteByPrimaryKey(creqpickmid);
    }
    @Override
    public int deleteChildrenVoById(String creqpickmid){
        MmReqpickmBExample example = new MmReqpickmBExample();
        MmReqpickmBExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(creqpickmid)) {
            criteria.andCreqpickmidEqualTo(creqpickmid);
        }
        return mmReqpickmBMapper.deleteByExample(example);
    }

    /**
     * 配料单的添加
     * @param mmReqpickm
     * @param lstMmReqpickmB
     * @return
     */
    @Override
    public int insertMmReqpickm(MmReqpickm mmReqpickm,List<MmReqpickmB> lstMmReqpickmB) {
        int res =0;
        try {
            res = mmReqpickmMapper.insert(mmReqpickm);
            for (int i = 0; i < lstMmReqpickmB.size(); i++) {
                MmReqpickmB itemVo = lstMmReqpickmB.get(i);
                 mmReqpickmBMapper.insert(itemVo);
            }
        } catch (Exception e) {
            res = 0;
        }
        return res;
    }

    /**
     * 修改配料单
     * @param mmReqpickm
     * @return
     */
    @Override
    public int updateMmReqpickm(MmReqpickm mmReqpickm) {
        return mmReqpickmMapper.updateByPrimaryKey(mmReqpickm);
    }

    @Override
    public int updateMmReqpickmB(List<MmReqpickmB> lstMmReqpickmB) {
        int res =0;
        for (int i = 0; i < lstMmReqpickmB.size(); i++) {
            MmReqpickmB itemVo = lstMmReqpickmB.get(i);
            res =mmReqpickmBMapper.updateByPrimaryKey(itemVo);
        }
        return res;
    }

    /**
     * 根据成产订单查询对应的物料信息
     * @param
     * @return
     */
    @Override
    public List<MmReqpickmB> selectMmReqpickmBByComid(String cmoid) {
        MmReqpickmBExample example = new MmReqpickmBExample();
        MmReqpickmBExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(cmoid)) {
            criteria.andCmoidEqualTo(cmoid);
        }
        return mmReqpickmBMapper.selectByExampleSelective(example,result);

    }


}
